package com.xuan.temper;

import com.alibaba.fastjson.JSON;
import com.xuan.temper.entity.Data;
import com.xuan.temper.entity.ExponeEntity;
import com.xuan.temper.entity.ReturnData;
import com.xuan.temper.utils.sendPost;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建人： king xuan
 * @创建时间：2020/8/20
 * @描述：
 */
public class zhishu {
    public static void main(String[] args) {
        zhishu.calculationTemper();
    }

    public static void calculationTemper() {


        String url = "https://open.lixinger.com/api/a/index/fundamental";
        String stockCodes[] = {"000016"};
        String metricsList[] = {"pe_ttm.mcw", "pb.mcw"};


        Map<String, Object> params = new HashMap<>();
        params.put("token", "8b5f363a-417a-4ac8-98ad-c2cfbd12a7fd");
        params.put("startDate", "2010-08-20");
        params.put("endDate", "2020-08-20");
        params.put("stockCodes", stockCodes);
        params.put("metricsList", metricsList);
        String paramsJson = JSON.toJSONString(params); // 将参数转换为json字符串
        //System.out.println(paramsJson);

        String vale = sendPost.sendPost(url, paramsJson);
        ReturnData jsonValue = JSON.parseObject(vale, ReturnData.class);
        System.out.println(vale);
        List<Data> data = jsonValue.getData();
        //pe
        ArrayList<Double> peList = new ArrayList<>();
        //pb
        ArrayList<Double> pbList = new ArrayList<>();
        ArrayList<ExponeEntity> dateList = new ArrayList<>();
        for (Data datum : data) {
            String stockCode = datum.getStockCode();
            Date date = datum.getDate();
            Double pe_ttm = datum.getPe_ttm().getMcw();
            Double pb = datum.getPb().getMcw();
            peList.add(pe_ttm);
            pbList.add(pb);
            ExponeEntity ExponeEntity = new ExponeEntity();
            ExponeEntity.setDate(date);
            ExponeEntity.setStockCode(stockCode);
            ExponeEntity.setPb(pb);
            ExponeEntity.setPe_ttm(pe_ttm);
            dateList.add(ExponeEntity);

        }
        Collections.sort(peList);
        Collections.sort(pbList);
        //数组大小
        Double peListSize = Double.valueOf(peList.size());
        Double pbListSize = Double.valueOf(pbList.size());
        //间隔因数
        Double i = 1 / peListSize;
            for(int j=0;j<pbList.size();j++){
                double pbValue = pbList.get(j);
                double peValue = peList.get(j);
                double pbTemper = (j * i)*100;
                double peTemper = (j * i)*100;
                for (ExponeEntity ExponeEntity : dateList) {
                    if (pbValue== ExponeEntity.getPb()){
                        ExponeEntity.setPbTemper(pbTemper);
                    }
                    if (peValue== ExponeEntity.getPe_ttm()){
                        ExponeEntity.setPeTemper(peTemper);
                    }
                }
            }
        SimpleDateFormat sd=new SimpleDateFormat("yyyy年MM月dd日");
        for (ExponeEntity ExponeEntity : dateList) {
            System.out.println("当前指数代码为: " + ExponeEntity.getStockCode());
            System.out.println("日期 = " + sd.format(ExponeEntity.getDate()));
            System.out.println("pe值 = " + ExponeEntity.getPe_ttm());
            System.out.println("pe温度 = " + ExponeEntity.getPeTemper());
            System.out.println("pb值 = " + ExponeEntity.getPb());
            System.out.println("pb温度 = " + ExponeEntity.getPbTemper());
            System.out.println("指数温度 = " + (ExponeEntity.getPbTemper()+ ExponeEntity.getPeTemper())/2);
            System.out.println("指数ROE = " +(ExponeEntity.getPbTemper()/ ExponeEntity.getPeTemper()) *100+"%");
            System.out.println("------------------------------");
        }
    }

}
